package com.ruoyi.teaching.service.impl;

import java.util.List;

import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaStudentMapper;
import com.ruoyi.teaching.domain.YaStudent;
import com.ruoyi.teaching.service.IYaStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-01
 */
@Service
public class YaStudentServiceImpl implements IYaStudentService {
  @Autowired
  private YaStudentMapper yaStudentMapper;

  @Autowired
  private RemoteUserService remoteUserService;

  /**
   * 查询学生信息
   *
   * @param id 学生信息主键
   * @return 学生信息
   */
  @Override
  public YaStudent selectYaStudentById(Long id) {
    return yaStudentMapper.selectYaStudentById(id);
  }

  /**
   * 查询学生信息列表
   *
   * @param yaStudent 学生信息
   * @return 学生信息
   */
  @Override
  public List<YaStudent> selectYaStudentList(YaStudent yaStudent) {
    return yaStudentMapper.selectYaStudentList(yaStudent);
  }

  /**
   * 新增学生信息
   *
   * @param yaStudent 学生信息
   * @return 结果
   */
  @Override
  public int insertYaStudent(YaStudent yaStudent) {
    // 查询当前最大ID并加1
    Long maxId = yaStudentMapper.selectMaxId();
    if (maxId == null) {
      maxId = 1L;
    } else {
      maxId += 1;
    }
    yaStudent.setId(maxId);

    /*SysUser user = new SysUser();
    user.setUserId(yaStudent.getId());
    user.setUserName(yaStudent.getUsername());
    user.setNickName(yaStudent.getName());
    user.setPhonenumber(yaStudent.getContactNumber());
    user.setSex(yaStudent.getGender().toString());
    user.setPassword("123456");

    R<Integer> r = remoteUserService.addStudent(user);
    if (HttpStatus.SUCCESS != r.getCode()) {
      throw new ServiceException("新增用户失败");
    }*/

    yaStudent.setCreateTime(DateUtils.getNowDate());
    return yaStudentMapper.insertYaStudent(yaStudent);
  }
  /*@GlobalTransactional
  @Transactional(rollbackFor = Exception.class)
  public int doInsertStudent(YaStudent yaStudent) {
    int result = yaStudentMapper.insertYaStudent(yaStudent);
    if (result <= 0) {
      throw new ServiceException("学生信息保存失败");
    }
    return result;
  }*/

  /**
   * 修改学生信息
   *
   * @param yaStudent 学生信息
   * @return 结果
   */
  @Override
  public int updateYaStudent(YaStudent yaStudent) {
    yaStudent.setUpdateTime(DateUtils.getNowDate());
    return yaStudentMapper.updateYaStudent(yaStudent);
  }

  /**
   * 批量删除学生信息
   *
   * @param ids 需要删除的学生信息主键
   * @return 结果
   */
  @Override
  public int deleteYaStudentByIds(Long[] ids) {
    return yaStudentMapper.deleteYaStudentByIds(ids);
  }

  /**
   * 删除学生信息信息
   *
   * @param id 学生信息主键
   * @return 结果
   */
  @Override
  public int deleteYaStudentById(Long id) {
    return yaStudentMapper.deleteYaStudentById(id);
  }
}
